package com.haralabidis.jdeveloper.extension.subversionmonitor.addin.preferences;

import oracle.ide.extension.RegisteredByExtension;

import oracle.javatools.data.HashStructure;
import oracle.javatools.data.HashStructureAdapter;
import oracle.javatools.data.PropertyStorage;

/**
 * <tt>SubversionMonitorPreferences</tt> data object.
 */
@RegisteredByExtension("com.haralabidis.jdeveloper.extension.subversionmonitor")
public final class SubversionMonitorPreferences extends HashStructureAdapter {
    public static final String DATA_KEY =
        "com.haralabidis.jdeveloper.extension.subversionmonitor.SubversionMonitorPreferences";

    private static final String KEY_URL = "SubversionMonitor.URL";
    private static final String KEY_USER = "SubversionMonitor.USER";
    private static final String KEY_PASSWORD = "SubversionMonitor.PASSWORD";
    private static final String KEY_TIMEOUT = "SubversionMonitor.TIMEOUT";
    private static final String KEY_MAX_REVISIONS_TO_VIEW = "SubversionMonitor.MAX_REVISIONS_TO_VIEW";
    private static final String KEY_DISABLED = "SubversionMonitor.DISABLED";

    private SubversionMonitorPreferences(HashStructure hash) {
        super(hash);
    }

    /**
     * Returns an instance of <tt>SubversionMonitorPreferences</tt>
     *
     * @param storage a storage object.
     * @throws NullPointerException if <tt>storage</tt> is <tt>null</tt>.
     */
    public static SubversionMonitorPreferences getInstance(PropertyStorage storage) {
        return new SubversionMonitorPreferences(findOrCreate(storage, DATA_KEY));
    }

    public String getSubversionUrl() {
        return getHashStructure().getString(KEY_URL, "svn://localhost:4701/");
    }

    public void setSubversionUrl(String url) {
        getHashStructure().putString(KEY_URL, url);
    }

    public String getSubversionUser() {
        return getHashStructure().getString(KEY_USER);
    }

    public void setSubversionUser(String user) {
        getHashStructure().putString(KEY_USER, user);
    }

    public String getSubversionPassword() {
        return getHashStructure().getString(KEY_PASSWORD);
    }

    public void setSubversionPassword(String password) {
        getHashStructure().putString(KEY_PASSWORD, password);
    }

    public int getMonitorTimeout() {
        int timeout = getHashStructure().getInt(KEY_TIMEOUT, 60);
        return timeout == 0 ? 60 : timeout;
    }

    public void setMonitorTimeout(int timeout) {
        getHashStructure().putInt(KEY_TIMEOUT, timeout);
    }

    public boolean getMonitorDisabled() {
        return getHashStructure().getBoolean(KEY_DISABLED, true);
    }

    public void setMonitorDisabled(boolean disabled) {
        getHashStructure().putBoolean(KEY_DISABLED, disabled);
    }

    public int getMaxRevisionsToView() {
        int maxRevisions = getHashStructure().getInt(KEY_MAX_REVISIONS_TO_VIEW, 500);
        return maxRevisions == 0 ? 500 : maxRevisions;
    }

    public void setMaxRevisionsToView(int maxRevisions) {
        getHashStructure().putInt(KEY_MAX_REVISIONS_TO_VIEW, maxRevisions);
    }

}
